package yja.gzw.app.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import yja.gzw.app.R
import yja.gzw.app.activity.ZaoShuju

class FabuAdapter (private val context: Context, private var list: MutableList<ZaoShuju>, private val listener: OnItemClickListener): RecyclerView.Adapter<RecyclerView.ViewHolder>(){
    private var view: View?=null
    interface OnItemClickListener {
        fun onItemClick(position: Int, uri: ZaoShuju)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        view= LayoutInflater.from(parent.context).inflate(R.layout.item_view_fabu,parent,false)
        Log.e("图片地址展示","走到了2")
        return ViewHodeView(view!!)


    }

    override fun getItemCount(): Int {
      return  list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if(holder is ViewHodeView){

            if(list[position].leixing==2){

                if(position==9){

                    holder.yulan.visibility=View.GONE
                    holder. shanchu.visibility=View.GONE
                    holder.tianjiatu.visibility=View.GONE
                }else{
                    if(list[position].leixing==2){
                        holder.yulan.visibility=View.GONE
                        holder. shanchu.visibility=View.GONE

                    }else{

                        holder.tianjiatu.visibility=View.VISIBLE
                        holder.yulan.visibility=View.VISIBLE
                        holder. shanchu.visibility=View.VISIBLE
                        holder.tianjiatu.setImageResource(R.mipmap.tianjitu)
                    }


                }




            }else{
                val requestOptions = RequestOptions()
                    .transform(CenterCrop(), RoundedCorners(15)) // 设置圆角半径为16像素
                Glide.with(context).load("http://117ai.32.hzgqapp.com/"+list[position].tupian)
                    .apply(requestOptions)
                    .into(holder.tianjiatu)



            }

            holder.shanchu.setOnClickListener {

                list.removeAt(position)

                // 通知适配器数据集已更改
                notifyItemRemoved(position)
                notifyItemRangeChanged(position, list.size - position)
            }



            holder.tianjiatu.setOnClickListener {
                listener.onItemClick(position, list[position])
            }

        }

    }


    inner class ViewHodeView(itemView: View):RecyclerView.ViewHolder(itemView){
        val tianjiatu= itemView.findViewById<ImageView>(R.id.tianjiatu)
        val shanchu=itemView.findViewById<ImageView>(R.id.shanchu)
        val yulan=itemView.findViewById<TextView>(R.id.yulan)


    }

}